<?php
/***************************************************************************
 * 
 * Copyright (c) 2010 , Inc. All Rights Reserved
 * $Id$:ActionController.php,2010/06/07 13:06:28 
 * 
 **************************************************************************/
 
 
 
/**
 * @file ActionController.php
 * @author huqingping
 * @date 2010/06/07 13:06:28
 * @version 1.0 
 * @brief 
 *  
 **/

final class ActionController extends PageController
{
	function _before()
	{
		if (empty($this->request->userData['_currentuser'])) {
		//	throw new Exception('hapn.u_login');
		}
		//var_dump($this->request->userData['_user']);
	}

	function index($ar)
	{
		var_dump($ar);
		echo "may be wrong\n";
	}

	function cookie()
	{
		if ($this->get('del')) {
			$this->response->delCookie('redirect');
			throw new Exception('hapn.fatal');
		}
		$cookie = $this->request->getCookie('redirect');
		if ($cookie) {
			var_dump($cookie);
		} elseif ($this->get('set')) {
			$this->response->setCookie('redirect','value'.time());
		}
	}

	function showget()
	{
		echo($this->get('k'));
		echo($this->getu('k'));
	}

	function exp()
	{
		//throw new Exception("perm.u_input_bopid:[61929683]_don't_have_permanent_pwid:[1784]");
		echo $a;
	}

	function dbtrx()
	{
		$proxy = ApiProxy::getProxy('moddemo');
		$ret = $proxy->demoFunc(1,2);
		$this->set('trxDemoFunc',$ret);
	}

	function dbmysql()
	{
		$db = Db::get('mall_mis');
		$items = $db->table('t_mis_item1')->where(array('iid'=>array(1,null)))->limit(0,2)->get();
		$this->set('first_2_item',$items);

		$db2 = Db::get('mall_std_mis');
		$items = $db2->table('t_mis_sp')->where(array('sp_id'=>array(1,null)))->order('sp_id',false)->limit(0,2)->get();
		$this->set('last_2_sp',$items);
	}
	
	function dbsysta()
	{
		$db = Db::get('mall01');
		$shops = $db->table('t_shop')->field(array('shop_uname'))->where(array('sid'=>1))->get();
		$this->set('shops',$shops);
	}

	function data()
	{
		foreach($this->request->inputs as $key=>$value) {
			$this->response->set($key,$value);
		}		
	}

	function apiproxy()
	{
		$proxy = ApiProxy::getProxy('moddemo');
		$ret = $proxy->demoFunc(1,2);
		$this->set('demoFunc',$ret);

		$ret = $proxy->demoFunc2(1);
		$this->set('demoFunc2',$ret);
	}

	function httprpcdemo()
	{
		$proxy = ApiProxy::getProxy('httprpcdemo');
		$ret = $proxy->demoFunc(1,2);
		$this->set('rpcdemoFunc',$ret);

		$ret = $proxy->demoFunc2(1);
		$this->set('rpcdemoFunc2',$ret);
	}

	function rpcdemo()
	{
		$proxy = ApiProxy::getProxy('dal');
		$ret = $proxy->select(array(
			//按照后端模块需要的格式写
			'sql'=>"select shop_uname from t_shop where sid = 1",
			'best'=>1,
			'retry'=>1
			));
		$this->set('dal-select',$ret);
	}

	function error()
	{
		$t = ApiProxy::getProxy('test');
		$t->test();
	}

	function xml()
	{
		$this->set('d1',array(
			array(1,2,3,4),
			array('k'=>1,'2','3','c'=>1),
			array(2,'d'=>'34')
		));
		$this->set('d2','2');
		$this->set('d3',array(
			array(array(1),2,3,'d'=>4),
			array(2,array('d'=>'34'))
		));
		$this->set('d4',array(
			array('k'=>1,'k2'=>2),
			array('k'=>2,'k2'=>3)
		));
	}

	function smarty()
	{
		$this->setView('demo/smarty');
	}
	function view()
	{
		$this->setView('demo/post');
	}
	function _post()
	{
		foreach($this->request->inputs as $key=>$value) {
			$this->response->set($key,$value);
		}		
		if (isset($this->request->userData['antispam'])) {
			foreach($this->request->userData['antispam'] as $key=>$value) {
				$this->response->set('_anti_'.$key,$value);
			}		
		}

	}

	protected function genVcode($type,$ext=null)
	{
		$vcode = MCCom::load('Vcode');
		if (!$vcode->genNew($type,$this->uid,$ext)) {
			//生成失败
			throw new Exception('hapn.vcode gen fail');
		}
	}

	protected function checkVcode($type,$ext=null)
	{
		$vcodeValue = $this->get('_vcode');
		$vcode = MCCom::load('Vcode');
		$ret = $vcode->verify($vcodeValue,$type,$this->uid,$ext);
		if ($ret !== 'success') {
			throw new Exception('hapn.u_vcode');
		}
	}

	function vcode()
	{
		$this->uid = 1;
		$type = $this->get('k');
		$ext = $this->get('e');
		$set = $this->debug?$this->get('set'):0;
		$vcode = MCCom::load('Vcode');
		if ($set) {
			$vcode->genNew($type,$this->uid,$ext);
			$this->set('gen','ok');
		} else {
			$imgdata = $vcode->getCaptchaPic($type,$this->uid,$ext);
			if (!$imgdata) {
				throw new Exception('hapn.u_vcode missing vcode value');
			}
			$this->response->setRaw($imgdata['content']);
			$this->response->setHeader('Content-Type: '.$imgdata['header']['Content-Type']);
			$this->response->setNoCache();
		}
	}

}





/* vim: set ts=4 sw=4 sts=4 tw=100 noet: */
?>
